﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using matlab_function;
using System.Drawing.Imaging;



namespace Image_Test1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Inverse_Button.Visible = false;
            Open_Button.Visible = false;
            //this.WindowState = FormWindowState.Maximized;
            //this.FormBorderStyle = FormBorderStyle.None;
            //this.TopMost = true;
        }
        
        public Color[,] Buffer;
        public Bitmap ImageFile;
        public double[,] R_Real, R_Imag, G_Real, G_Imag, B_Real, B_Imag;
        public double[,] R, G, B;

        public void Intiallize(Bitmap Image)
        {
            ImageFile =Image;
            //for (int i = 0; i < Image.Width; i++)
            //{
            //    for (int j = 0; j < Image.Height; j++)
            //    {
            //        ImageFile.SetPixel(j,i,Image.GetPixel(i,j));
            //    }
            //}
            DisplayPicture1.Image = ImageFile;
            R = new double[ImageFile.Height, ImageFile.Width];
            G = new double[ImageFile.Height, ImageFile.Width];
            B = new double[ImageFile.Height, ImageFile.Width];
            Buffer = new Color[ImageFile.Height, ImageFile.Width];

            for (int i = 0; i < ImageFile.Height; i++)
            {
                for (int j = 0; j < ImageFile.Width; j++)
                {
                    Buffer[i, j] = ImageFile.GetPixel(j, i);
                    R[i, j] = Buffer[i, j].R;
                    G[i, j] = Buffer[i, j].G;
                    B[i, j] = Buffer[i, j].B;
                }
            }


        }
        private void Open_Button_Click(object sender, EventArgs e)
        {
            string File_Name = "";
            OpenFileDialog.Title = "Open Image";
            OpenFileDialog.FileName = "";
            OpenFileDialog.Filter = "All Files|*.*|BITMAPS|*.bmp|JPEG Images|*.jpg|GIF Images|*.gif|PNG Images|*.png";



            if (OpenFileDialog.ShowDialog() == DialogResult.Cancel)
            {
                MessageBox.Show("Cancel");
            }
            else
            {
                File_Name = OpenFileDialog.FileName;
                ImageFile = new Bitmap(File_Name);
                DisplayPicture1.Image = ImageFile;
                Buffer = new Color[ImageFile.Height,ImageFile.Width];
                R = new double[ImageFile.Height,ImageFile.Width];
                G = new double[ImageFile.Height, ImageFile.Width];
                B = new double[ImageFile.Height, ImageFile.Width];

                for (int i = 0; i < ImageFile.Height; i++)
                {
                    for (int j = 0; j < ImageFile.Width; j++)
                    {
                        Buffer[i, j] = ImageFile.GetPixel(j, i);
                        R[i, j] = Buffer[i, j].R;
                        G[i, j] = Buffer[i, j].G;
                        B[i, j] = Buffer[i, j].B;
                    }
                }
                //Start();
            }
        }

       

        public static double[,] Normalization(double[,] norm_arr,int height, int width)
        {
            double max = 0;
            double min = 1000;

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    if (norm_arr[i, j] > max)
                        max = norm_arr[i, j];
                    if (norm_arr[i, j] < min)
                        min = norm_arr[i, j];
                }
            }
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    norm_arr[i, j] = ((norm_arr[i, j] - min) / (max - min)) * 255;
                }
            }
            return norm_arr;


        }

        private void Inverse_Button_Click(object sender, EventArgs e)
        {
            //EngMATAccess mat = new EngMATAccess();
            mat mat1 = new mat();
            double[,] R1 = new double[ImageFile.Height, ImageFile.Width];
            double[,] G1 = new double[ImageFile.Height, ImageFile.Width];
            double[,] B1 = new double[ImageFile.Height, ImageFile.Width];

            #region Deal With Matlab

            #region Old
            /*
            
            mat.SetMatrix("R_Real", R_Real);
            mat.SetMatrix("R_Imag", R_Imag);

            mat.SetMatrix("G_Real", G_Real);
            mat.SetMatrix("G_Imag", G_Imag);

            mat.SetMatrix("B_Real", B_Real);
            mat.SetMatrix("B_Imag", B_Imag);

            mat.Evaluate("R = ReverseTrans(R_Real,R_Imag); G = ReverseTrans(G_Real,G_Imag); B = ReverseTrans(B_Real,B_Imag);");

            mat.GetMatrix("R", ref R);
            mat.GetMatrix("G", ref  G);
            mat.GetMatrix("B", ref  B);
            */
            #endregion

            MWNumericArray new_r = (MWNumericArray)mat1.ReverseTrans((MWNumericArray)R_Real, (MWNumericArray)R_Imag);
            R1 = (double[,])new_r.ToArray();
            MWNumericArray new_g = (MWNumericArray)mat1.ReverseTrans((MWNumericArray)G_Real, (MWNumericArray)G_Imag);
            G1 = (double[,])new_g.ToArray();
            MWNumericArray new_b = (MWNumericArray)mat1.ReverseTrans((MWNumericArray)B_Real, (MWNumericArray)B_Imag);
            B1 = (double[,])new_b.ToArray();

            #endregion

            #region Display Image after Inverse
            Bitmap Image = new Bitmap(ImageFile.Width, ImageFile.Height);
            for (int i = 0; i < ImageFile.Height; i++)
                for (int j = 0; j < ImageFile.Width; j++)
                    Image.SetPixel(j, i, Color.FromArgb((int)R1[i, j], (int)G1[i, j], (int)B1[i, j]));

            DisplayPicture6.Image = Image; 
            #endregion
            
        }

        private void LowPass_RadioButton_CheckedChanged(object sender, EventArgs e)
        {
            HighPass_RadioButton.Checked = !LowPass_RadioButton.Checked;
        }

        private void HighPass_RadioButton_CheckedChanged(object sender, EventArgs e)
        {
            LowPass_RadioButton.Checked = !HighPass_RadioButton.Checked;
        }

        private enum TypeOfFilter { LowPass, HighPass }

        private void Ideal_Button_Click(object sender, EventArgs e)
        {
            TypeOfFilter Filter;
            if (LowPass_RadioButton.Checked == true)
                Filter = TypeOfFilter.LowPass;
            else
                Filter = TypeOfFilter.HighPass;
            
            //EngMATAccess mat = new EngMATAccess();
            mat mat1=new mat();

            R_Real = new double[ImageFile.Height, ImageFile.Width];
            R_Imag = new double[ImageFile.Height, ImageFile.Width];

            G_Real = new double[ImageFile.Height, ImageFile.Width];
            G_Imag = new double[ImageFile.Height, ImageFile.Width];

            B_Real = new double[ImageFile.Height, ImageFile.Width];
            B_Imag = new double[ImageFile.Height, ImageFile.Width];

            #region Deal with Matlap

            #region Old
            /*
            mat.SetMatrix("R", R);
            mat.SetMatrix("G", G);
            mat.SetMatrix("B", B);

            mat.Evaluate("R_Real = ConvertReal(R); G_Real = ConvertReal(G); B_Real = ConvertReal(B);");
            mat.Evaluate("R_Imag = ConvertImag(R); G_Imag = ConvertImag(G); B_Imag = ConvertImag(B);");


            mat.GetMatrix("R_Real", ref R_Real);
            mat.GetMatrix("R_Imag", ref R_Imag);

            mat.GetMatrix("G_Real", ref G_Real);
            mat.GetMatrix("G_Imag", ref G_Imag);

            mat.GetMatrix("B_Real", ref B_Real);
            mat.GetMatrix("B_Imag", ref B_Imag);
            */
            #endregion

            MWNumericArray new_r = (MWNumericArray)mat1.ConvertReal((MWNumericArray)R);
            R_Real = (double[,])new_r.ToArray();
            MWNumericArray new_g = (MWNumericArray)mat1.ConvertReal((MWNumericArray)G);
            G_Real = (double[,])new_g.ToArray();
            MWNumericArray new_b = (MWNumericArray)mat1.ConvertReal((MWNumericArray)B);
            B_Real = (double[,])new_b.ToArray();

            new_r = (MWNumericArray)mat1.ConvertImag((MWNumericArray)R);
            R_Imag = (double[,])new_r.ToArray();
            new_g = (MWNumericArray)mat1.ConvertImag((MWNumericArray)G);
            G_Imag = (double[,])new_g.ToArray();
            new_b = (MWNumericArray)mat1.ConvertImag((MWNumericArray)B);
            B_Imag = (double[,])new_b.ToArray();

            #endregion

            double[,] D = new double[ImageFile.Height, ImageFile.Width];
            double[,] H = new double[ImageFile.Height, ImageFile.Width];
            double D0_value = double.Parse(D_TextBox.Text);

            #region Equation
            if (Filter == TypeOfFilter.LowPass)
            {
                for (int i = 0; i < ImageFile.Height; i++)
                {
                    for (int j = 0; j < ImageFile.Width; j++)
                    {
                        D[i, j] = Math.Sqrt(Math.Pow((i - (ImageFile.Height / 2.0)), 2) + Math.Pow((j - (ImageFile.Width / 2.0)), 2));
                        H[i, j] = ((D[i, j] <= D0_value) ? 1 : 0);
                    }
                }
            }
            else
            {
                for (int i = 0; i < ImageFile.Height; i++)
                {
                    for (int j = 0; j < ImageFile.Width; j++)
                    {
                        D[i, j] = Math.Sqrt(Math.Pow((j - (ImageFile.Width / 2)), 2) + Math.Pow((i - (ImageFile.Height / 2)), 2));
                        H[i, j] = ((D[i, j] <= D0_value) ? 0 : 1);
                    }
                }
            }
            #endregion

            #region Result of equation (H) * Real & Imagin
            for (int i = 0; i < ImageFile.Height; i++)
            {
                for (int j = 0; j < ImageFile.Width; j++)
                {
                    R_Real[i, j] *= H[i, j];
                    R_Imag[i, j] *= H[i, j];

                    G_Real[i, j] *= H[i, j];
                    G_Imag[i, j] *= H[i, j];

                    B_Real[i, j] *= H[i, j];
                    B_Imag[i, j] *= H[i, j];
                }
            }
            #endregion

            double[,] R1 = new double[ImageFile.Height, ImageFile.Width];
            double[,] G1 = new double[ImageFile.Height, ImageFile.Width];
            double[,] B1 = new double[ImageFile.Height, ImageFile.Width];

            #region Deal With Matlab

            #region Old
            /*
            mat.SetMatrix("R_Real", R_Real);
            mat.SetMatrix("R_Imag", R_Imag);

            mat.SetMatrix("G_Real", G_Real);
            mat.SetMatrix("G_Imag", G_Imag);

            mat.SetMatrix("B_Real", B_Real);
            mat.SetMatrix("B_Imag", B_Imag);

            mat.Evaluate("R = ReverseTrans(R_Real,R_Imag); G = ReverseTrans(G_Real,G_Imag); B = ReverseTrans(B_Real,B_Imag);");

            mat.GetMatrix("R", ref R);
            mat.GetMatrix("G", ref  G);
            mat.GetMatrix("B", ref  B);
            */
            #endregion

            new_r = (MWNumericArray)mat1.ReverseTrans((MWNumericArray)R_Real, (MWNumericArray)R_Imag);
            R1 = (double[,])new_r.ToArray();
            new_g = (MWNumericArray)mat1.ReverseTrans((MWNumericArray)G_Real, (MWNumericArray)G_Imag);
            G1 = (double[,])new_g.ToArray();
            new_b = (MWNumericArray)mat1.ReverseTrans((MWNumericArray)B_Real, (MWNumericArray)B_Imag);
            B1 = (double[,])new_b.ToArray();

            #endregion

            #region Normalization
            if (Filter == TypeOfFilter.LowPass)
            {
                for (int i = 0; i < ImageFile.Height; i++)
                {
                    for (int j = 0; j < ImageFile.Width; j++)
                    {
                        R1[i, j] = CutOff(R1[i, j]);
                        G1[i, j] = CutOff(G1[i, j]);
                        B1[i, j] = CutOff(B1[i, j]);
                    }
                }
            }
            else if (Filter == TypeOfFilter.HighPass)
            {
                R1 = Normalization(R1, ImageFile.Height, ImageFile.Width);
                G1 = Normalization(G1, ImageFile.Height, ImageFile.Width);
                B1 = Normalization(B1, ImageFile.Height, ImageFile.Width);
            }
            #endregion

            #region Display Image After Filter

            Bitmap Image = new Bitmap(ImageFile.Width, ImageFile.Height);
            for (int i = 0; i < ImageFile.Height; i++)
                for (int j = 0; j < ImageFile.Width; j++)
                    Image.SetPixel(j, i, Color.FromArgb((int)R1[i, j], (int)G1[i, j], (int)B1[i, j]));

            DisplayPicture6.Image = Image;

            #endregion

            //mat.Close();
              
        }

        private void Gaussian_Button_Click(object sender, EventArgs e)
        {
            TypeOfFilter Filter;
            if (LowPass_RadioButton.Checked == true)
                Filter = TypeOfFilter.LowPass;
            else
                Filter = TypeOfFilter.HighPass;
            
            //EngMATAccess mat = new EngMATAccess();
            mat mat1=new mat();

            R_Real = new double[ImageFile.Height, ImageFile.Width];
            R_Imag = new double[ImageFile.Height, ImageFile.Width];

            G_Real = new double[ImageFile.Height, ImageFile.Width];
            G_Imag = new double[ImageFile.Height, ImageFile.Width];

            B_Real = new double[ImageFile.Height, ImageFile.Width];
            B_Imag = new double[ImageFile.Height, ImageFile.Width];

            #region Deal with Matlap

            #region Old
            /*
            mat.SetMatrix("R", R);
            mat.SetMatrix("G", G);
            mat.SetMatrix("B", B);

            mat.Evaluate("R_Real = ConvertReal(R); G_Real = ConvertReal(G); B_Real = ConvertReal(B);");
            mat.Evaluate("R_Imag = ConvertImag(R); G_Imag = ConvertImag(G); B_Imag = ConvertImag(B);");


            mat.GetMatrix("R_Real", ref R_Real);
            mat.GetMatrix("R_Imag", ref R_Imag);

            mat.GetMatrix("G_Real", ref G_Real);
            mat.GetMatrix("G_Imag", ref G_Imag);

            mat.GetMatrix("B_Real", ref B_Real);
            mat.GetMatrix("B_Imag", ref B_Imag);
            */
            #endregion

            MWNumericArray new_r = (MWNumericArray)mat1.ConvertReal((MWNumericArray)R);
            R_Real = (double[,])new_r.ToArray();
            MWNumericArray new_g = (MWNumericArray)mat1.ConvertReal((MWNumericArray)G);
            G_Real = (double[,])new_g.ToArray();
            MWNumericArray new_b = (MWNumericArray)mat1.ConvertReal((MWNumericArray)B);
            B_Real = (double[,])new_b.ToArray();

            new_r = (MWNumericArray)mat1.ConvertImag((MWNumericArray)R);
            R_Imag = (double[,])new_r.ToArray();
            new_g = (MWNumericArray)mat1.ConvertImag((MWNumericArray)G);
            G_Imag = (double[,])new_g.ToArray();
            new_b = (MWNumericArray)mat1.ConvertImag((MWNumericArray)B);
            B_Imag = (double[,])new_b.ToArray();

            #endregion

            double[,] D = new double[ImageFile.Height, ImageFile.Width];
            double[,] H = new double[ImageFile.Height, ImageFile.Width];
            double D0_value = double.Parse(D_TextBox.Text);

            #region Equation
            if (Filter == TypeOfFilter.LowPass)
            {
                for (int i = 0; i < ImageFile.Height; i++)
                {
                    for (int j = 0; j < ImageFile.Width; j++)
                    {
                        D[i, j] = Math.Sqrt(Math.Pow((j - (ImageFile.Width / 2)), 2) + Math.Pow((i - (ImageFile.Height / 2)), 2));
                        H[i, j] = Math.Exp(-(Math.Pow(D[i,j],2))/(2*D0_value*D0_value));
                    }
                }
            }
            else
            {
                for (int i = 0; i < ImageFile.Height; i++)
                {
                    for (int j = 0; j < ImageFile.Width; j++)
                    {
                        D[i, j] = Math.Sqrt(Math.Pow((j - (ImageFile.Width / 2)), 2) + Math.Pow((i - (ImageFile.Height / 2)), 2));
                        H[i, j] = 1-(Math.Exp(-(Math.Pow(D[i, j], 2)) / (2 * D0_value * D0_value)));
                    }
                }
            }
            #endregion

            #region Result of equation (H) * Real & Imagain
            for (int i = 0; i < ImageFile.Height; i++)
            {
                for (int j = 0; j < ImageFile.Width; j++)
                {
                    R_Real[i, j] *= H[i, j];
                    R_Imag[i, j] *= H[i, j];

                    G_Real[i, j] *= H[i, j];
                    G_Imag[i, j] *= H[i, j];

                    B_Real[i, j] *= H[i, j];
                    B_Imag[i, j] *= H[i, j];
                }
            }
            #endregion

            double[,] R1 = new double[ImageFile.Height, ImageFile.Width];
            double[,] G1 = new double[ImageFile.Height, ImageFile.Width];
            double[,] B1 = new double[ImageFile.Height, ImageFile.Width];

            #region Deal With Matlab

            #region Old
            /*
            mat.SetMatrix("R_Real", R_Real);
            mat.SetMatrix("R_Imag", R_Imag);

            mat.SetMatrix("G_Real", G_Real);
            mat.SetMatrix("G_Imag", G_Imag);

            mat.SetMatrix("B_Real", B_Real);
            mat.SetMatrix("B_Imag", B_Imag);

            mat.Evaluate("R = ReverseTrans(R_Real,R_Imag); G = ReverseTrans(G_Real,G_Imag); B = ReverseTrans(B_Real,B_Imag);");

            mat.GetMatrix("R", ref R);
            mat.GetMatrix("G", ref  G);
            mat.GetMatrix("B", ref  B);
            */
            #endregion

            new_r = (MWNumericArray)mat1.ReverseTrans((MWNumericArray)R_Real, (MWNumericArray)R_Imag);
            R1 = (double[,])new_r.ToArray();
            new_g = (MWNumericArray)mat1.ReverseTrans((MWNumericArray)G_Real, (MWNumericArray)G_Imag);
            G1 = (double[,])new_g.ToArray();
            new_b = (MWNumericArray)mat1.ReverseTrans((MWNumericArray)B_Real, (MWNumericArray)B_Imag);
            B1 = (double[,])new_b.ToArray();

            #endregion

            #region Normalization & Cuttoff
            if (Filter == TypeOfFilter.LowPass)
            {
                for (int i = 0; i < ImageFile.Height; i++)
                {
                    for (int j = 0; j < ImageFile.Width; j++)
                    {
                        R1[i, j] = CutOff(R1[i, j]);
                        G1[i, j] = CutOff(G1[i, j]);
                        B1[i, j] = CutOff(B1[i, j]);
                    }
                }
            }
            else if (Filter == TypeOfFilter.HighPass)
            {
                R1 = Normalization(R1, ImageFile.Height, ImageFile.Width);
                G1 = Normalization(G1, ImageFile.Height, ImageFile.Width);
                B1 = Normalization(B1, ImageFile.Height, ImageFile.Width);
            }
            #endregion

            #region Display Image After Filter

            Bitmap Image = new Bitmap(ImageFile.Width, ImageFile.Height);
            for (int i = 0; i < ImageFile.Height; i++)
                for (int j = 0; j < ImageFile.Width; j++)
                    Image.SetPixel(j, i, Color.FromArgb((int)R1[i, j], (int)G1[i, j], (int)B1[i, j]));

            DisplayPicture6.Image = Image;

            #endregion

            //mat.Close();
              
        }

        private void ButterWorth_Button_Click(object sender, EventArgs e)
        {
            TypeOfFilter Filter;
            if (LowPass_RadioButton.Checked == true)
                Filter = TypeOfFilter.LowPass;
            else
                Filter = TypeOfFilter.HighPass;
            
            //EngMATAccess mat = new EngMATAccess();
            mat mat1=new mat();

            R_Real = new double[ImageFile.Height, ImageFile.Width];
            R_Imag = new double[ImageFile.Height, ImageFile.Width];

            G_Real = new double[ImageFile.Height, ImageFile.Width];
            G_Imag = new double[ImageFile.Height, ImageFile.Width];

            B_Real = new double[ImageFile.Height, ImageFile.Width];
            B_Imag = new double[ImageFile.Height, ImageFile.Width];

            #region Deal with Matlap
            #region Old
            /*
            mat.SetMatrix("R", R);
            mat.SetMatrix("G", G);
            mat.SetMatrix("B", B);

            mat.Evaluate("R_Real = ConvertReal(R); G_Real = ConvertReal(G); B_Real = ConvertReal(B);");
            mat.Evaluate("R_Imag = ConvertImag(R); G_Imag = ConvertImag(G); B_Imag = ConvertImag(B);");


            mat.GetMatrix("R_Real", ref R_Real);
            mat.GetMatrix("R_Imag", ref R_Imag);

            mat.GetMatrix("G_Real", ref G_Real);
            mat.GetMatrix("G_Imag", ref G_Imag);

            mat.GetMatrix("B_Real", ref B_Real);
            mat.GetMatrix("B_Imag", ref B_Imag);
            */
            #endregion
            MWNumericArray new_r = (MWNumericArray)mat1.ConvertReal((MWNumericArray)R);
            R_Real = (double[,])new_r.ToArray();
            MWNumericArray new_g = (MWNumericArray)mat1.ConvertReal((MWNumericArray)G);
            G_Real = (double[,])new_g.ToArray();
            MWNumericArray new_b = (MWNumericArray)mat1.ConvertReal((MWNumericArray)B);
            B_Real = (double[,])new_b.ToArray();

            new_r = (MWNumericArray)mat1.ConvertImag((MWNumericArray)R);
            R_Imag = (double[,])new_r.ToArray();
            new_g = (MWNumericArray)mat1.ConvertImag((MWNumericArray)G);
            G_Imag = (double[,])new_g.ToArray();
            new_b = (MWNumericArray)mat1.ConvertImag((MWNumericArray)B);
            B_Imag = (double[,])new_b.ToArray();

            #endregion

            double[,] D = new double[ImageFile.Height, ImageFile.Width];
            double[,] H = new double[ImageFile.Height, ImageFile.Width];
            double D0_value = double.Parse(D_TextBox.Text);
            double N_Value = double.Parse(N_TextBox.Text);

            #region Equation
            if (Filter == TypeOfFilter.LowPass)
            {
                for (int i = 0; i < ImageFile.Height; i++)
                {
                    for (int j = 0; j < ImageFile.Width; j++)
                    {
                        D[i, j] = Math.Sqrt(Math.Pow((j - (ImageFile.Width / 2)), 2) + Math.Pow((i - (ImageFile.Height / 2)), 2));
                        H[i, j] = 1 / (1 + Math.Pow((D[i, j] / D0_value), 2*N_Value));
                    }
                }
            }
            else
            {
                for (int i = 0; i < ImageFile.Height; i++)
                {
                    for (int j = 0; j < ImageFile.Width; j++)
                    {
                        D[i, j] = Math.Sqrt(Math.Pow((j - (ImageFile.Width / 2)), 2) + Math.Pow((i - (ImageFile.Height / 2)), 2));
                        H[i, j] = 1 - (1 / (1 + Math.Pow((D[i, j] / D0_value), 2 * N_Value)));
                    }
                }
            }
            #endregion

            #region Result of equation (H) * Real & Imagain
            for (int i = 0; i < ImageFile.Height; i++)
            {
                for (int j = 0; j < ImageFile.Width; j++)
                {
                    R_Real[i, j] *= H[i, j];
                    R_Imag[i, j] *= H[i, j];

                    G_Real[i, j] *= H[i, j];
                    G_Imag[i, j] *= H[i, j];

                    B_Real[i, j] *= H[i, j];
                    B_Imag[i, j] *= H[i, j];
                }
            }
            #endregion

            double[,] R1 = new double[ImageFile.Height, ImageFile.Width];
            double[,] G1 = new double[ImageFile.Height, ImageFile.Width];
            double[,] B1 = new double[ImageFile.Height, ImageFile.Width];

            #region Deal With Matlab

            #region Old
            /*
            mat.SetMatrix("R_Real", R_Real);
            mat.SetMatrix("R_Imag", R_Imag);

            mat.SetMatrix("G_Real", G_Real);
            mat.SetMatrix("G_Imag", G_Imag);

            mat.SetMatrix("B_Real", B_Real);
            mat.SetMatrix("B_Imag", B_Imag);

            mat.Evaluate("R = ReverseTrans(R_Real,R_Imag); G = ReverseTrans(G_Real,G_Imag); B = ReverseTrans(B_Real,B_Imag);");

            mat.GetMatrix("R", ref R);
            mat.GetMatrix("G", ref  G);
            mat.GetMatrix("B", ref  B);
            */
            #endregion

            new_r = (MWNumericArray)mat1.ReverseTrans((MWNumericArray)R_Real, (MWNumericArray)R_Imag);
            R1 = (double[,])new_r.ToArray();
            new_g = (MWNumericArray)mat1.ReverseTrans((MWNumericArray)G_Real, (MWNumericArray)G_Imag);
            G1 = (double[,])new_g.ToArray();
            new_b = (MWNumericArray)mat1.ReverseTrans((MWNumericArray)B_Real, (MWNumericArray)B_Imag);
            B1 = (double[,])new_b.ToArray();

            #endregion

            #region  & Cutoff
            if (Filter == TypeOfFilter.LowPass)
            {
                for (int i = 0; i < ImageFile.Height; i++)
                {
                    for (int j = 0; j < ImageFile.Width; j++)
                    {
                        R1[i, j] = CutOff(R1[i, j]);
                        G1[i, j] = CutOff(G1[i, j]);
                        B1[i, j] = CutOff(B1[i, j]);
                    }
                }
            }
            else if (Filter == TypeOfFilter.HighPass)
            {
                R1 = Normalization(R1, ImageFile.Height, ImageFile.Width);
                G1 = Normalization(G1, ImageFile.Height, ImageFile.Width);
                B1 = Normalization(B1, ImageFile.Height, ImageFile.Width);
            }
            #endregion

            #region Display Image After Filter

            Bitmap Image = new Bitmap(ImageFile.Width, ImageFile.Height);
            for (int i = 0; i < ImageFile.Height; i++)
                for (int j = 0; j < ImageFile.Width; j++)
                    Image.SetPixel(j, i, Color.FromArgb((int)R1[i, j], (int)G1[i, j], (int)B1[i, j]));

            DisplayPicture6.Image = Image;

            #endregion

            //mat.Close();
        }

        private double CutOff(double Value)
        {
            if (Value > 255)
                Value = 255;
            else if (Value < 0)
                Value = 0;
            return Value;
        }

        private void DisplayPicture6_MouseDoubleClick(object sender, MouseEventArgs e)
        {

            SaveFileDialog.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
            SaveFileDialog.Title = "Save an Image File";


            if (SaveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK && SaveFileDialog.FileName != "")
            {
                try
                {
                    using (Bitmap bitmap = new Bitmap(DisplayPicture6.Image))
                    {
                        bitmap.Save(SaveFileDialog.FileName, ImageFormat.Jpeg);
                    }
                    MessageBox.Show("Picture Saved Successfully.");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("An Error has occured: \n" + ex.Message);
                }
            }
            else
                MessageBox.Show("Error");
        }
    }
}
